#include "EventHandler.h"
#include "DisplayPrimitive.h"
#include "GameManager.h"

//Handeling events
EventHandler::EventHandler(GameManager * inMngr)
	: mManager(inMngr) {}


void EventHandler::HandleEvent( const EventRec & inEvent ) {
	switch (inEvent.mType) {
		case INPUT_EVENT:
			HandleInputEvent( static_cast<const InputEventRec &>(inEvent) );
			break;
		case COLLISION_EVENT:
			HandleCollisionEvent( static_cast<const CollisionEventRec &>(inEvent) );
			break;
		default:
			break;
	}
}

void EventHandler::HandleInputEvent( const InputEventRec & inEvent ) {
	assert(inEvent.mType == INPUT_EVENT);
	Camera * cam;
	GameManager * mngr = mManager;
	PLAYER_STATE_T playerState = mManager->mPlayer->GetState();

	//These are the input events that occur.
	switch(inEvent.mAction){
		case INPUT_MOVE_LEFT:
			mManager->mPlayer->SetInputDirection(glm::vec2(-1.0f, 0.0));
			if (inEvent.start == true) {
				mManager->mPlayer->SetStateBit(PLAYER_STATE_WALKING);
			}
			else {
				mManager->mPlayer->RemoveStateBit(PLAYER_STATE_WALKING);
			}
			break;
		case INPUT_MOVE_RIGHT:
			mManager->mPlayer->SetInputDirection(glm::vec2(1.0f, 0.0));	
			if (inEvent.start == true) {
				mManager->mPlayer->SetStateBit(PLAYER_STATE_WALKING);
			}
			else {
				mManager->mPlayer->RemoveStateBit(PLAYER_STATE_WALKING);
			}
			break;
		case INPUT_ZOOM_IN:
		{
			Camera * cam = mManager->GetDisplayer()->GetCamera();
			cam->IncrementPixelToMeterRatio( -1.0 );
			break;
		}
		case INPUT_ZOOM_OUT:
		{
			Camera * cam = mManager->GetDisplayer()->GetCamera();
			cam->IncrementPixelToMeterRatio( 1.0 );
			break;
		}
		//Shoot requires location (inferred from mouse position)
		case INPUT_SHOOT:
		{
			//XXX Need to create a target move event so that target is set everytime the mouse is moved,
			//even if aimed at same place.
			if (inEvent.start == true) {
				mManager->mPlayer->SetTarget(inEvent.mouseInfo.mWorldPos);
				mManager->mPlayer->SetStateBit(PLAYER_STATE_SHOOTING);
			}
			else {
				mManager->mPlayer->RemoveStateBit(PLAYER_STATE_SHOOTING);
			}
			break;
		}
	default:
		break;
	}
}

void EventHandler::HandleCollisionEvent( const CollisionEventRec & inEvent ) {
	switch(inEvent.mAction) {
	case COLLISION_EVENT_GUN_SHOT :
		{
			printf("Collision at %f %f\n", inEvent.point.x, inEvent.point.y);
			SpriteDisplayPrimitiveDef sDef;
			sDef.doKeep = true;
			sDef.tex = mManager->testTex;
			sDef.worldPos = glm::vec2(inEvent.point.x-1, inEvent.point.y-1);
			sDef.worldWH = glm::vec2(2, 2);
			sDef.isActive = true;
			

			mManager->mDisplayManager->AddFromDef( &sDef);
		}
		break;
	default:
		break;
	}
}

